package com.tommy.ddp.grid
{
	import com.tommy.ddp.GridDataManager;
	import com.tommy.ddp.box.DdpBox;
	import com.tommy.ddp.definition.DDPConfig;
	import com.tommy.ddp.definition.GridSkinFactory;
	import com.tommy.ddp.grid.skin.GlassGridSkin;
	import com.tommy.ddp.grid.skin.IGridSkin;
	import com.tommy.ddp.grid.skin.NormalGridSkin;
	
	import flash.geom.Point;
	
	import starling.display.MovieClip;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.filters.BlurFilter;
	import starling.filters.FragmentFilter;

	/**
	 * 对对碰背景格子
	 * @author tommy
	 * 
	 */	
	public class DdpGrid extends Sprite
	{
		private var gridSkin:IGridSkin;
		private var _row:int;
		private var _column:int;
		/**
		 * 是否被占用了，寻路的时候预定了这个位置，其它格子寻不到该点了
		 */		
		public var isOccupied:Boolean;
		/**
		 * 是否有格子在上面移动
		 */		
		public var isMoving:Boolean;
		//|||串联管道连接的点，，还没处理
		public var connectGrid:DdpGrid;
		
		
		public function DdpGrid()
		{
			super();
			
			this.touchable = false;
			
			setGridSkin();
		}
		/**
		 * 设置外观样式
		 * 
		 */		
		public function setGridSkin(type:uint = 0):void
		{
			if(gridSkin)
			{
				if(type == gridSkin.type)
					return;
				else
					gridSkin.dispose();
			}
			//
			gridSkin = GridSkinFactory.getGridSkin(type);
			
			if(gridSkin.mc)
				this.addChild(gridSkin.mc);
		}
		
		
		public function get row():int
		{
			return _row;
		}
		
		public function get column():int
		{
			return _column;
		}
		
		/**
		 * 第几行/Y
		 * @param v
		 * 
		 */		
		public function set row(v:int):void
		{
			_row = v;
			this.y = _row*DDPConfig.GRID_SIZE_H;
		}
		/**
		 * 第几列/X
		 * @param v
		 * 
		 */		
		public function set column(v:int):void
		{
			_column = v;
			this.x = _column*DDPConfig.GRID_SIZE_W;
		}
		
		public function get skin():IGridSkin
		{
			return gridSkin;
		}
		
		/**
		 * 当前格子是否可用来填放棋子
		 * @return 
		 * 
		 */		
		public function get isAvaliable():Boolean
		{
			//不计算当时格子上是否有棋子，因为刚开始的时候需要寻路，寻路过程是无视棋子的，直接找到阻挡格子
			return gridSkin.isAvaliable;
		}
		
		/**
		 * 是否可被拿来寻路
		 * @return 
		 * 
		 */		
		public function get isCanFind():Boolean
		{
			//return gridSkin.isCanBefind && !isOccupied;
			return isAvaliable && !isOccupied;
		}
		
		
		override public function dispose():void
		{
			super.dispose();
			
			connectGrid = null;
			
			gridSkin.dispose();
			gridSkin = null;
		}
		/**
		 * 自身位置的棋子消失时的格子效果处理
		 * 
		 */		
		public function clear():void
		{
			if(gridSkin.type == GridSkinFactory.WIRE_NET)
			{
				this.parent.addChildAt(this,0);
			}
			var nextType:uint = gridSkin.nextTypeByClear;
			setGridSkin(nextType);
		}
		/**
		 * 附近棋子消失时的格子效果处理
		 * 
		 */		
		public function punch():void
		{
			var nextType:uint = gridSkin.nextTypeByPunch;
			setGridSkin(nextType);
		}
	}
}